﻿#include "httpservice.h"
#include "testcontroller.h"

#include <QCoreApplication>
#include <QHttpServer>
#include <QHttpServerRequest>
#include <QHttpServerResponse>
#include <QJsonObject>

static inline QString host(const QHttpServerRequest &request)
{
    return request.headers()[QStringLiteral("Host")].toString();
}


int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    TestController testController;
    HttpService::getInstance().installController(&testController,"test");
    HttpService::getInstance().start();
//    system("pause");

//    QHttpServer httpServer;
//    httpServer.route("/", []() {
//        return "Hello world";
//    });

//    httpServer.route("/query", [] (const QHttpServerRequest &request) {
//        return QString("%1/query/").arg(host(request));
//    });

//    httpServer.route("/query/", [] (qint32 id, const QHttpServerRequest &request) {
//        return QString("%1/query/%2").arg(host(request)).arg(id);
//    });

//    httpServer.route("/query/<arg>/log", [] (qint32 id, const QHttpServerRequest &request) {
//        return QString("%1/query/%2/log").arg(host(request)).arg(id);
//    });

//    httpServer.route("/query/<arg>/log/", [] (qint32 id, float threshold,
//                                              const QHttpServerRequest &request) {
//        return QString("%1/query/%2/log/%3").arg(host(request)).arg(id).arg(threshold);
//    });

//    httpServer.route("/user/", [] (const qint32 id) {
//        return QString("User %1").arg(id);
//    });

//    httpServer.route("/user/<arg>/detail", [] (const qint32 id) {
//        return QString("User %1 detail").arg(id);
//    });

//    httpServer.route("/user/<arg>/detail/", [] (const qint32 id, const qint32 year) {
//        return QString("User %1 detail year - %2").arg(id).arg(year);
//    });

//    httpServer.route("/headers", [](const QHttpServerRequest &request) {
//        // 获取所有的响应头信息
//        QJsonObject obj;
//                QVariantMap headers = request.headers();
//                for (auto it = headers.begin(); it != headers.end(); ++it) {
//                    qDebug() << "Header:" << it.key() << "Value:" << it.value();
//                    obj.insert(it.key(),it.value().toJsonValue());
//                }

//        return obj;
//    });

//    httpServer.route("/json/", [] {
//        return QJsonObject{
//            {
//                {"key1", "1"},
//                {"key2", "2"},
//                {"key3", "3"}
//            }
//        };
//    });

//    httpServer.route("/assets/<arg>", [] (const QUrl &url) {
//        return QHttpServerResponse::fromFile(QStringLiteral(":/assets/%1").arg(url.path()));
//    });

//    httpServer.route("/remote_address", [](const QHttpServerRequest &request) {
//        return request.remoteAddress().toString();
//    });


//    const auto port = httpServer.listen(QHostAddress::Any,8080);
//    if (!port) {
//        qDebug() << QCoreApplication::translate(
//                "QHttpServerExample", "Server failed to listen on a port.");
//        return 0;
//    }

//    qDebug() << QCoreApplication::translate(
//            "QHttpServerExample", "Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)").arg(port);

    return app.exec();
}
